const db = require('./connect');
// ------管理员的登录注册------------------------------------------------------
// 登录
adLogin = (req, res) => {
	
	let {user, pass} = req.body;
	
	let _data = [user, pass];
	console.log(_data);
	let sql = 'SELECT pass FROM login WHERE user="' + _data[0] + '"';
	
	let callback = (err, data) => {
		if(err){
			res.send({
				code: 404,
				msg: '登入失败' + err
			})
		}else{
			//判断账号
			if(data[0] === undefined){//表示账号不存在
				res.send({
					code: 304,
					msg: '账号不存在'
				})
			}else{//账号存在
				//判断密码是否正确
				if(data[0].pass === _data[1]){//表示密码,权限正确
					res.send({
						code: 200,
						msg: '登入成功',
						isLogin: 1
					})
				}else{//表示密码
					res.send({
						code: 300,
						msg: '密码错误',
						isLogin: 0
					})
				}
			}
		}
	}
	
	db.sqlConnect(sql, _data, callback);
}
// 上传图片
uploadImage = (req, res) => {
  // 获取图片信息
  let { user, pass, image } = req.file;
  
  // 构造SQL语句
  let sql = `INSERT INTO register (user, pass, image) VALUES (?, ?, ?)`;
  
  // 构造参数数组
  let params = [user, pass, image];
  
  // 执行SQL语句
  db.sqlConnect(sql, params, (err, data) => {
    if (err) {
      res.send({
        code: 500,
        msg: '图片上传失败' + err
      });
    } else {
      res.send({
        code: 200,
        msg: '图片上传成功',
      });
    }
  });
}

module.exports = {
  adLogin,
  uploadImage
}